<template>
  <div class="table-box">
    <ProTable ref="proRef" :columns="columns" :pagination="false" :request-api="getArea">
      <template #tableHeader="scope">
        <el-button type="primary" @click="opera(undefined)">创建面积</el-button>
        <el-button type="danger" @click="deleteApi(scope.selectedListIds)" :disabled="scope.selectedListIds.length === 0"
          >批量删除</el-button
        >
      </template>
      <template #opera="{ row }">
        <el-button link :icon="EditPen" @click="opera(row)" type="primary">编辑</el-button>
        <el-button link :icon="Delete" type="primary" @click="deleteApi([row.id])">删除</el-button>
      </template>
    </ProTable>
    <areaDrawer ref="areaRef"></areaDrawer>
  </div>
</template>

<script setup lang="ts">
import ProTable from "@/components/ProTable/index.vue";
import { ColumnProps } from "@/components/ProTable/interface";
import { ref } from "vue";
import { createArea, editArea, getArea, deleteArea } from "@/api/modules/area";
import { EditPen, Delete } from "@element-plus/icons-vue";
import areaDrawer from "../components/areaDrawer.vue";
import { useHandleData } from "@/hooks/useHandleData";

const areaRef = ref<InstanceType<typeof areaDrawer> | null>();

const proRef = ref<InstanceType<typeof ProTable> | null>();

const deleteApi = async (ids: any) => {
  await useHandleData(deleteArea, { ids }, "删除所选内容");
  proRef.value?.getTableList();
};

const columns: ColumnProps[] = [
  { prop: "sort", label: "排序值" },
  { prop: "areaType", label: "类型" },
  { prop: "min", label: "最小值" },
  { prop: "max", label: "最大值" },
  { prop: "opera", label: "操作" }
];

const opera = (row: any) => {
  const params: any = {
    row: row ? row : { sort: 1, areaType: 0, min: 0, max: 0 },
    api: row ? editArea : createArea,
    getTable: proRef.value?.getTableList
  };
  areaRef.value?.acceptParams(params);
};
</script>

<style></style>
